# -*- coding=utf-8
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
from https.UploadPaths import save_file_path_to_json
from jdk.openAi.repCli import get_open_ai_to_title
import json
import log.logger as log
import random

secret_id = "AKID3vyuw7IK8ua4KK5nLhsrsqeMvNdjfMpE"
secret_key = "KRN8oSuBHB3nLf2GF1bBdwTHySy8KI51"
region = "ap-guangzhou"
bucket_name = "xfx-video-1255813649"
cos_domain = f"https://{bucket_name}.cos.ap-guangzhou.myqcloud.com"
token = None
try:
    config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token)  # 获取配置对象
    client = CosS3Client(config)
except Exception as err:
    print(f"构建CosS3Client失败 secret_id：{secret_id}")


# 本地路径 简单上传

def put_tencent_file(text_parm, template_id, local_file_path, upload_folder, selected_gifs, selected_png,
                     selected_music, selected_avatars):
    # 获取ai的文案
    text, emos = get_open_ai_to_title(template_id, text_parm, selected_gifs)
    new_file_pat = f"{upload_folder}/{template_id}/{text}.mp4"
    src = f"{cos_domain}/{new_file_pat}"
    json_data = create_json_data(text_parm, src, template_id, text, selected_gifs, selected_png, selected_music,
                                 selected_avatars)
    php_url = save_file_path_to_json(json_data)
    data_dict = json.loads(php_url)
    # 从字典中获取 "code" 值
    code_value = data_dict.get("code", None)
    if code_value == 0:
        # 上传文件
        try:
            with open(local_file_path, 'rb') as fp:
                response = client.put_object(
                    Bucket=bucket_name,
                    Body=fp,
                    Key=new_file_pat,
                )
            log.log_put(16, None, None)
        except Exception as e:
            log.log_put(17, None, None)
            print(e)
    else:
        # 如果 phpurl["code"] 不等于 0，则重新调用 put_tencent_file 方法
        print(f"视频记录上传返回参数异常，详情:{php_url}，\n正在重试中.....")
        return put_tencent_file(text_parm, template_id, local_file_path, upload_folder, selected_gifs, selected_png,
                                selected_music, selected_avatars)
    num_objects = random.randint(2, 5)
    # 获取随机数量的对象
    random_objects = random.sample(emos, num_objects)
    data_dict['label_list'] = random_objects
    return data_dict


def create_json_data(text_parm, src, template_id, title, selected_gifs, selected_png, selected_music, selected_avatars):
    code = template_id if template_id not in [1, 2, 3] else 51
    text_id = text_parm["id"] if text_parm else None
    # 如果selected_avatars不为空，则遍历获取id，否则使用空列表
    avatars_ids = [avatar['id'] for avatar in selected_avatars] if selected_avatars else None

    json_data = {
        'src': src,
        'bqb_writing': text_id,
        'bqb_look': [gif['id'] for gif in selected_gifs],
        'bqb_back': selected_png['id'],
        'bqb_music': selected_music['id'],
        'title': title,
        'model_id': code,
        'bqb_virtually': avatars_ids

    }
    return json_data


if __name__ == '__main__':
    print("1")
